package org.xuan.poi.plus;


import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.*;
import org.xuan.poi.plus.word.WordParagraphsUtils;
import org.xuan.poi.plus.word.WordTablesUtils;
import org.xuan.poi.plus.word.obj.CustomXWPFDocument;
import org.xuan.poi.plus.word.utils.RandomUtil;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

public class Word {

    /**
     * 进行文字替换
     * @param param    需要替换的参数
     * @param template 模板
     */
    public static XWPFDocument generateWord(Map<String, Object> param, String template) throws Exception {
        CustomXWPFDocument doc = null;
        try {
            OPCPackage pack = POIXMLDocument.openPackage(template);//通过路径获取word模板
            doc = new CustomXWPFDocument(pack);
            //通过InputStream 获取模板，此方法适用于jar包部署
            //  doc = new CustomXWPFDocument(template);
            if (param != null && param.size() > 0) {
                //处理段落
                WordParagraphsUtils.replaceInAllParagraphs(doc.getParagraphs(), param);
                //处理表格
                WordTablesUtils.replaceInTables(doc.getTables(), param);
            }
        } catch (Exception e) {
            throw e;
        }
        return doc;
    }

    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("year1,");
        stringBuffer.append("caseno1,");
        stringBuffer.append("companyname1,");
        stringBuffer.append("companyname2,");
        stringBuffer.append("mainname1,");
        stringBuffer.append("secondname1,");
        stringBuffer.append("amount1,");
        stringBuffer.append("locktime1,");
        stringBuffer.append("year2,");
        stringBuffer.append("caseno2,");
        stringBuffer.append("companyname3,");
        stringBuffer.append("companyaddress1,");
        stringBuffer.append("companyphone1,");
        stringBuffer.append("legalname1,");
        stringBuffer.append("licenceno1,");
        stringBuffer.append("checktime1,");
        stringBuffer.append("carno1,");
        stringBuffer.append("cartype1,");
        stringBuffer.append("deviceno1,");
        stringBuffer.append("numb1,");
        stringBuffer.append("weight1,");
        stringBuffer.append("qualitylimit1,");
        stringBuffer.append("overrun1,");
        stringBuffer.append("overraterun1,");
        stringBuffer.append("weireal1,");
        stringBuffer.append("overrealrun1,");
        stringBuffer.append("level1,");
        stringBuffer.append("amount2,");
        stringBuffer.append("endtime19,");
        stringBuffer.append("companyname4,");
        stringBuffer.append("companyname5,");
        stringBuffer.append("drivername1,");
        stringBuffer.append("year3,");
        stringBuffer.append(" caseno3,");
        stringBuffer.append("receipttimefir1,");
        stringBuffer.append("endtime8,");
        stringBuffer.append("year7,");
        stringBuffer.append("caseno7,");
        stringBuffer.append("companyname6,");
        stringBuffer.append("checktime8,");
        stringBuffer.append("companyname7,");
        stringBuffer.append("legalname2,");
        stringBuffer.append("companyaddress2,");
        stringBuffer.append("companyphone2,");
        stringBuffer.append("licenceno2,");
        stringBuffer.append("checktime2,");
        stringBuffer.append("carno2,");
        stringBuffer.append("cartype2,");
        stringBuffer.append("deviceno2,");
        stringBuffer.append("numb2,");
        stringBuffer.append("weight2,");
        stringBuffer.append("qualitylimit2,");
        stringBuffer.append("overrun2,");
        stringBuffer.append("overraterun2,");
        stringBuffer.append("weireal2,");
        stringBuffer.append("overrealrun2,");
        stringBuffer.append("locktime4,");
        stringBuffer.append("mainname2,");
        stringBuffer.append("secondname2,");
        stringBuffer.append("locktime5,");
        stringBuffer.append("notetime1,");
        stringBuffer.append("mainname4,");
        stringBuffer.append("secondname4,");
        stringBuffer.append("secondname5,");
        stringBuffer.append("drivername3,");
        stringBuffer.append("driverid2,");
        stringBuffer.append("driverphone1,");
        stringBuffer.append("driveraddress1,");
        stringBuffer.append("mainname5,");
        stringBuffer.append("secondname6,");
        stringBuffer.append("mainno1,");
        stringBuffer.append("secondno1,");
        stringBuffer.append("drivername4,");
        stringBuffer.append("driversex1,");
        stringBuffer.append("driverage1,");
        stringBuffer.append("driveraddress2,");
        stringBuffer.append("driverid3,");
        stringBuffer.append("carno3,");
        stringBuffer.append("checktime4,");
        stringBuffer.append("deviceno4,");
        stringBuffer.append("carno4,");
        stringBuffer.append("cartype10,");
        stringBuffer.append("goodsname1,");
        stringBuffer.append("startaddress1,");
        stringBuffer.append("endaddress1,");
        stringBuffer.append("carno5,");
        stringBuffer.append("transport,");
        stringBuffer.append("carno6,");
        stringBuffer.append("numb4,");
        stringBuffer.append("weight0,");
        stringBuffer.append("checktime5,");
        stringBuffer.append("deviceno6,");
        stringBuffer.append("carno8,");
        stringBuffer.append("numb5,");
        stringBuffer.append("weight4,");
        stringBuffer.append("qualitylimit7,");
        stringBuffer.append("overrun4,");
        stringBuffer.append("overraterun4,");
        stringBuffer.append("weireal3,");
        stringBuffer.append("overrealrun3,");
        stringBuffer.append("companyname8,");
        stringBuffer.append("mainname3,");
        stringBuffer.append("secondname3,");
        stringBuffer.append("companyname11,");
        stringBuffer.append("companyaddress3,");
        stringBuffer.append("companyphone3,");
        stringBuffer.append("legalname3,");
        stringBuffer.append("licenceno3,");
        stringBuffer.append("checktime3,");
        stringBuffer.append("carno9,");
        stringBuffer.append("cartype3,");
        stringBuffer.append("deviceno3,");
        stringBuffer.append("numb3,");
        stringBuffer.append("weight3,");
        stringBuffer.append("qualitylimit3,");
        stringBuffer.append("overrun3,");
        stringBuffer.append("overraterun3,");
        stringBuffer.append("mainname6,");
        stringBuffer.append("secondname7,");
        stringBuffer.append("weireal4,");
        stringBuffer.append("overrealrun4,");
        stringBuffer.append("companyname12,");
        stringBuffer.append("amount3,");
        stringBuffer.append("level3,");
        stringBuffer.append("endtime3,");
        stringBuffer.append("endtime4,");
        stringBuffer.append("endtime5,");
        stringBuffer.append("year4,");
        stringBuffer.append("caseno4,");
        stringBuffer.append("companyname14,");
        stringBuffer.append("checktime6,");
        stringBuffer.append("carno10,");
        stringBuffer.append("cartype5,");
        stringBuffer.append("deviceno7,");
        stringBuffer.append("numb6,");
        stringBuffer.append("weight5,");
        stringBuffer.append("qualitylimit5,");
        stringBuffer.append("overrun5,");
        stringBuffer.append("overraterun5,");
        stringBuffer.append("weireal5,");
        stringBuffer.append("overrealrun5,");
        stringBuffer.append("level4,");
        stringBuffer.append("amount4,");
        stringBuffer.append("endtime6,");
        stringBuffer.append("companyname15,");
        stringBuffer.append("companyname16,");
        stringBuffer.append("drivername5,");
        stringBuffer.append("year5,");
        stringBuffer.append("caseno5,");
        stringBuffer.append("receipttimesec1,");
        stringBuffer.append("endtime10,");
        stringBuffer.append("year8,");
        stringBuffer.append("caseno8,");
        stringBuffer.append("companyname23,");
        stringBuffer.append("endtime11,");
        stringBuffer.append("companyname17,");
        stringBuffer.append("grosscasetime1,");
        stringBuffer.append("mainname9,");
        stringBuffer.append("mainname14,");
        stringBuffer.append("mainname7,");
        stringBuffer.append("mainname8,");
        stringBuffer.append("checktime10,");
        stringBuffer.append("companyname18,");
        stringBuffer.append("carno13,");
        stringBuffer.append("deviceno9,");
        stringBuffer.append("numb8,");
        stringBuffer.append("weight9,");
        stringBuffer.append("qualitylimit8,");
        stringBuffer.append("overrun7,");
        stringBuffer.append("overraterun7,");
        stringBuffer.append("weireal8,");
        stringBuffer.append("overrealrun8,");
        stringBuffer.append("amount5,");
        stringBuffer.append("endtime12,");
        stringBuffer.append("mainname10,");
        stringBuffer.append("level2,");
        stringBuffer.append("amount6,");
        stringBuffer.append("amount7,");
        stringBuffer.append("declaretime1,");
        stringBuffer.append("drivername6,");
        stringBuffer.append("driversex2,");
        stringBuffer.append("companyname20,");
        stringBuffer.append("driverphone2,");
        stringBuffer.append("driveraddress3,");
        stringBuffer.append("mainname11,");
        stringBuffer.append("mainno2,");
        stringBuffer.append("secondname14,");
        stringBuffer.append("secondno2,");
        stringBuffer.append("secondname11,");
        stringBuffer.append("checktime9,");
        stringBuffer.append("carno12,");
        stringBuffer.append("cartype8,");
        stringBuffer.append("deviceno10,");
        stringBuffer.append("numb9,");
        stringBuffer.append("weight8,");
        stringBuffer.append("overrun8,");
        stringBuffer.append("overraterun8,");
        stringBuffer.append("mainname13,");
        stringBuffer.append("secondname12,");
        stringBuffer.append("weireal7,");
        stringBuffer.append("overrealrun7,");
        stringBuffer.append("companyname21,");
        stringBuffer.append("endtime14,");
        stringBuffer.append("year6,");
        stringBuffer.append("caseno6,");
        stringBuffer.append("amount8,");
        stringBuffer.append("companyname22,");
        stringBuffer.append("companyname13,");
        stringBuffer.append("companyname19,");
        stringBuffer.append("companyaddress4,");
        stringBuffer.append("companyphone4,");
        stringBuffer.append("legalname4,");
        stringBuffer.append("licenceno4,");
        stringBuffer.append("checktime7,");
        stringBuffer.append("carno11,");
        stringBuffer.append("cartype6,");
        stringBuffer.append("deviceno8,");
        stringBuffer.append("numb7,");
        stringBuffer.append("weight6,");
        stringBuffer.append("qualitylimit6,");
        stringBuffer.append("overrun6,");
        stringBuffer.append("overraterun6,");
        stringBuffer.append("weireal6,");
        stringBuffer.append("overrealrun6,");
        stringBuffer.append("amount9,");
        stringBuffer.append("endtime15,");
        stringBuffer.append("endtime16,");
        stringBuffer.append("endtime17,");
        stringBuffer.append("caseno0");

        String[] split = stringBuffer.toString().split(",");
        System.out.println(split.length);

        Map<String, Object> mp = new HashMap<String, Object>();

        for (int i = 0; i < split.length; i++) {
            mp.put("${" + split[i] + "}", "***");
        }

        try {
            // 模板文件
            String template = "C:/Users/ZGJTYF/Desktop/aa/111.docx";
            XWPFDocument doc = Word.generateWord(mp,template);
            // 输出文件
            String f = "C:/Users/ZGJTYF/Desktop/aa/" + RandomUtil.getRandomForIntegerBounded4(0, 100) + ".docx";
            System.out.println(f);
            OutputStream outputStream = new FileOutputStream(f);
            doc.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
